package org.flexharmony.eclipse.properties;

import static org.flexharmony.eclipse.properties.PropertiesConstants.*;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class PropertiesPanel extends AbstractHarmonyPropertiesPanel
{
  private Text flexSrcDirText;
  
  private Button additiveSyncBtn;

  private Button cleanUpdatesBtn;

  private String initialFlexSrcDir;
  
  private boolean initialAdditiveOnly;
  
  private boolean initialCleanUpdates;
  
  public PropertiesPanel(IProject project, Composite parent)
  {
    super(project, parent);
    
    createLayout();
    loadPropertyValues();
  }
  
  public void save() throws CoreException
  {
    setProperty(FLEX_SRC_DIR_NAME, flexSrcDirText.getText());
    setProperty(ADDITIVE_SYNC, Boolean.toString(additiveSyncBtn.getSelection()));
    setProperty(CLEAN_UPDATES, Boolean.toString(cleanUpdatesBtn.getSelection()));
  }
  
  public void restoreDefaults() 
  {
    try
    {
      setProperty(FLEX_SRC_DIR_NAME, null);
      setProperty(ADDITIVE_SYNC, null);
      setProperty(CLEAN_UPDATES, null);
    }
    catch (CoreException cexc)
    {
      cexc.printStackTrace();
    }
    
    loadPropertyValues();
  }
  
  public boolean valuesChanged()
  {
    return (!flexSrcDirText.getText().trim().equals(initialFlexSrcDir) 
        || (additiveSyncBtn.getSelection() != initialAdditiveOnly)
        || (cleanUpdatesBtn.getSelection() != initialCleanUpdates));
  }
  
  private void createLayout()
  {
    GridLayout layout = new GridLayout();
    layout.marginTop = 5;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.numColumns = 2;
    layout.verticalSpacing = 20;
    setLayout(layout);

    Label label = new Label(this, SWT.NONE);
    label.setLayoutData(new GridData());
    label.setText("Flex Source Directory");
  
    flexSrcDirText = new Text(this, SWT.BORDER | SWT.SINGLE);
    flexSrcDirText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    
    additiveSyncBtn = new Button(this, SWT.CHECK | SWT.LEFT);
    additiveSyncBtn.setText("Additive Synchronization Only");
    GridData gd = new GridData();
    gd.horizontalSpan = 2;
    additiveSyncBtn.setLayoutData(gd);

    cleanUpdatesBtn = new Button(this, SWT.CHECK | SWT.LEFT);
    cleanUpdatesBtn.setText("Clean Updates");
    gd = new GridData();
    gd.horizontalSpan = 2;
    additiveSyncBtn.setLayoutData(gd);
  }
  
  private void loadPropertyValues()
  {
    try
    {
      initialFlexSrcDir = getPropertyString(FLEX_SRC_DIR_NAME);
      initialAdditiveOnly = Boolean.parseBoolean(getPropertyString(ADDITIVE_SYNC));
      initialCleanUpdates = Boolean.parseBoolean(getPropertyString(CLEAN_UPDATES));
      flexSrcDirText.setText(initialFlexSrcDir);
      additiveSyncBtn.setSelection(initialAdditiveOnly);
      cleanUpdatesBtn.setSelection(initialCleanUpdates);
    }
    catch (CoreException cexc)
    {
      cexc.printStackTrace();
    }
  }
}
